package vue.stage;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import metier.CategorieStage;
import metier.Stage;
import ctr.commun.CommunGest;
import ctr.stage.GestModifStage;

public class JPanModifStage extends JPanel{
private static final long serialVersionUID = 1L;
private JComboBox jcbNomCategorieHaut = null, jcbNomCategorieBas;
private JTextField jtfNomAnnee, tfDateDebut, tfDateFin;
private Stage unStage;
private String strAnnee;
private String titre;

public JPanModifStage(String titre ) {
	this.titre = titre;
	init();
}

public JPanModifStage(JPanModifStage jPanModifStage ) {
	this.titre = jPanModifStage.titre;
	this.jcbNomCategorieHaut = jPanModifStage.getCBNomCategorieHaut();
	this.strAnnee = jPanModifStage.getStr_Annee();
	this.unStage = jPanModifStage.getUnStage();
	init();
}

private void init(){
	this.setLayout(new GridLayout(2,1));
	this.setBorder(new TitledBorder(titre));
	
	//Panel du haut
	JPanel panelHaut = new JPanel();
	panelHaut.setLayout(new GridLayout(2,2));
	
	JPanel panelLabelHaut = new JPanel();
	panelLabelHaut.setLayout(new GridLayout(2,1));
	
	JLabel lblNomCategorieHaut = new JLabel("Categorie : ");
	JLabel lblAnneeHaut = new JLabel("Année");
	
	panelLabelHaut.add(lblAnneeHaut);
	panelLabelHaut.add(lblNomCategorieHaut);
	
	//Champ de saisie
	JPanel panelSaisiHaut = new JPanel();
	panelSaisiHaut.setLayout(new GridLayout(2,1));
	
	jtfNomAnnee = new JTextField(4);
	
	if (strAnnee!=null)
	{
		jtfNomAnnee.setText(strAnnee);
	}else
	 {
		jtfNomAnnee.setText("2012");
	 }
	
	jtfNomAnnee.addActionListener(new GestModifStage(this));
	
	//ComboBox
	//Recuperation de la liste des Categories
	CommunGest communGest = new CommunGest();
	ArrayList<CategorieStage> listeCatStage = communGest.getListeCategorieStage();
	
	if(jcbNomCategorieHaut == null)
	{
		jcbNomCategorieHaut = new JComboBox(listeCatStage.toArray());
	}
	
	panelSaisiHaut.add(jtfNomAnnee);
	panelSaisiHaut.add(jcbNomCategorieHaut);
	
	//Bouton
	JButton boutRechercher = new JButton("Rechercher");
	boutRechercher.addActionListener(new GestModifStage(this));
	
	JPanel panelBoutonRech = new JPanel();
	panelBoutonRech.add(boutRechercher);
	
	panelHaut.setBorder(new TitledBorder("Rechercher un stage"));
	panelHaut.setLayout(new GridBagLayout());
	GridBagConstraints gbc = new GridBagConstraints();
	gbc.weightx = 0;
	gbc.weighty = 0;
	gbc.gridwidth = 1;
	gbc.gridheight = 1;
	
	gbc.gridx = 0;
	gbc.gridy = 0;
	gbc.fill = GridBagConstraints.VERTICAL;
	panelHaut.add(panelLabelHaut, gbc);
	
	gbc.gridx = 1;
	gbc.gridy = 0;
	gbc.fill = GridBagConstraints.VERTICAL;
	panelHaut.add(panelSaisiHaut, gbc);
	
	gbc.gridx = 0;
	gbc.gridy = 2;
	gbc.gridwidth = 2;
	gbc.gridheight = 2;
	gbc.fill = GridBagConstraints.CENTER;
	panelHaut.add(panelBoutonRech, gbc);
	this.add(panelHaut);
	
	if(unStage != null)
	{
		//Panel du bas
		JPanel panelSaisiBas = new JPanel(new GridLayout(3,1));
		panelSaisiBas.setBorder(new TitledBorder("Modifier un stage"));
			
		JPanel panelLigne1 = new JPanel();
		JLabel stageSel = new JLabel("Vous avez selectionné le stage : "+unStage.getLibelleStage());
		panelLigne1.add(stageSel);
		panelSaisiBas.add(panelLigne1);
		
		JPanel panelLigne2 = new JPanel(new GridLayout(1,2));
		
		JPanel panLabelBas = new JPanel(new GridLayout(3,1));
		JLabel lblNouvCategorie = new JLabel("Nouvelle catégorie");
		JLabel lblNouvDateDebut = new JLabel("Date début");
		JLabel lblNouvDateFin = new JLabel("Date fin");
		panLabelBas.add(lblNouvCategorie);
		panLabelBas.add(lblNouvDateDebut);
		panLabelBas.add(lblNouvDateFin);
		
		panelLigne2.add(panLabelBas);
		
		JPanel panChampSaisiBas = new JPanel(new GridLayout(3,1));
		jcbNomCategorieBas = new JComboBox(listeCatStage.toArray());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		this.tfDateDebut = new JTextField(10);
		this.tfDateDebut.setText(simpleDateFormat.format(unStage.getDateDebut()));
		this.tfDateFin = new JTextField(10);
		this.tfDateFin.setText(simpleDateFormat.format(unStage.getDateDebut()));
		panChampSaisiBas.add(jcbNomCategorieBas);
		panChampSaisiBas.add(tfDateDebut);
		panChampSaisiBas.add(tfDateFin);
		
		panelLigne2.add(panChampSaisiBas);
		
		panelSaisiBas.add(panelLigne2);
		
		//Bouton
		JButton boutModifier = new JButton("Modifier");
		boutModifier.addActionListener(new GestModifStage(this));
		
		JPanel panelBoutonModif = new JPanel();
		panelBoutonModif.add(boutModifier);
		
		panelSaisiBas.add(panelBoutonModif);
		
		this.add(panelSaisiBas);
	}

}

//GETTERS et SETTERS
public JTextField getJTFDateDebutAff() {
	return tfDateDebut;
}

public JTextField getJTFDateFinAff() {
	return tfDateFin;
}

public JComboBox getJcbNomCategorieHaut() {
	return jcbNomCategorieHaut;
}

public JComboBox getJcbNomCategorieBas() {
	return jcbNomCategorieBas;
}

public JTextField getJtfNomAnnee() {
	return jtfNomAnnee;
}

public Stage getUnStage() {
	return unStage;
}

public String getStr_Annee() {
	return strAnnee;
}

//METHODES
public JComboBox getCBNomCategorieBas() {
return jcbNomCategorieBas;
}

public JComboBox getCBNomCategorieHaut() {
return jcbNomCategorieHaut;
}

public JTextField getTFNomAnnee() {
return jtfNomAnnee;
}

public void setUnStage(Stage unStage) {
	this.unStage = unStage;
}

}

